﻿namespace blackhouse.mojoPortalExtend
{
    using mojoPortal.Business;
    using mojoPortal.Web;
    using mojoPortal.Web.Framework;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web;

    public class EditModule
    {
        private HttpContext context;
        private mojoBasePage page;
        private HttpRequest request;
        private HttpResponse response;

        public EditModule(mojoBasePage page)
        {
            this.page = page;
            this.context = HttpContext.Current;
            if ((this.context != null) && (this.page != null))
            {
                this.request = page.Request;
                this.response = page.Response;
                if (!this.request.IsAuthenticated)
                {
                    SiteUtils.RedirectToLoginPage(page);
                }
                else
                {
                    SecurityHelper.DisableBrowserCache();
                    this.PageId = WebUtils.ParseInt32FromQueryString("pageid", -1);
                    this.ModuleId = WebUtils.ParseInt32FromQueryString("mid", -1);
                    this.CurrentModuleSettings = ModuleSettings.GetModuleSettings(this.ModuleId);
                    this.CurrentUser = SiteUtils.GetCurrentSiteUser();
                    this.CanEdit = this.UserCanEdit();
                    this.CanEditAsDraft = this.page.UserCanOnlyEditModuleAsDraft(this.ModuleId);
                    if (!(this.CanEdit || this.CanEditAsDraft))
                    {
                        SiteUtils.RedirectToAccessDeniedPage(this.page);
                    }
                }
            }
        }

        private bool UserCanEdit()
        {
            return (this.page.UserCanEditModule(this.ModuleId) && (this.ModuleId > 0));
        }

        public bool CanEdit { get; set; }

        public bool CanEditAsDraft { get; set; }

        public Hashtable CurrentModuleSettings { get; set; }

        public SiteUser CurrentUser { get; set; }

        public int ModuleId { get; set; }

        public int PageId { get; set; }
    }
}

